package xyz.scootaloo.oj.leetcode

import cn.hutool.core.lang.Console
import org.junit.Test
import xyz.scootaloo.oj.ds.TreeNode

/**
 * @author flutterdash@qq.com
 * @since 2021/4/28 17:13
 */
class P98 {

    fun isValidBST(root: TreeNode?): Boolean {
        return isValidBSTCore(root, Long.MIN_VALUE, Long.MAX_VALUE)
    }

    private fun isValidBSTCore(root: TreeNode?, minValue: Long, maxValue: Long): Boolean {
        if (root == null)
            return true
        if ((minValue >= root.`val`) or (maxValue <= root.`val`))
            return false

        return when {
            !isValidBSTCore(root.left, minValue, root.`val`.toLong()) -> false
            !isValidBSTCore(root.right, root.`val`.toLong(), maxValue) -> false
            else -> true
        }
    }

    @Test
    fun test0() {
        val node1 = TreeNode(Int.MAX_VALUE)
        val nodeL = TreeNode(1)
        node1.left = nodeL
        Console.log(isValidBST(node1))
    }

}
